package cn.iocoder.yudao.module.reservation.controller.admin.rechargeorder;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.reservation.controller.admin.rechargeorder.vo.*;
import cn.iocoder.yudao.module.reservation.dal.dataobject.rechargeorder.RechargeOrderDO;
import cn.iocoder.yudao.module.reservation.service.rechargeorder.RechargeOrderService;

@Tag(name = "管理后台 - 充值订单")
@RestController
@RequestMapping("/reservation/recharge-order")
@Validated
public class RechargeOrderController {

    @Resource
    private RechargeOrderService rechargeOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建充值订单")
    @PreAuthorize("@ss.hasPermission('reservation:recharge-order:create')")
    public CommonResult<Long> createRechargeOrder(@Valid @RequestBody RechargeOrderSaveReqVO createReqVO) {
        return success(rechargeOrderService.createRechargeOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新充值订单")
    @PreAuthorize("@ss.hasPermission('reservation:recharge-order:update')")
    public CommonResult<Boolean> updateRechargeOrder(@Valid @RequestBody RechargeOrderSaveReqVO updateReqVO) {
        rechargeOrderService.updateRechargeOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除充值订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('reservation:recharge-order:delete')")
    public CommonResult<Boolean> deleteRechargeOrder(@RequestParam("id") Long id) {
        rechargeOrderService.deleteRechargeOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得充值订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('reservation:recharge-order:query')")
    public CommonResult<RechargeOrderRespVO> getRechargeOrder(@RequestParam("id") Long id) {
        RechargeOrderDO rechargeOrder = rechargeOrderService.getRechargeOrder(id);
        return success(BeanUtils.toBean(rechargeOrder, RechargeOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得充值订单分页")
    @PreAuthorize("@ss.hasPermission('reservation:recharge-order:query')")
    public CommonResult<PageResult<RechargeOrderRespVO>> getRechargeOrderPage(@Valid RechargeOrderPageReqVO pageReqVO) {
        PageResult<RechargeOrderDO> pageResult = rechargeOrderService.getRechargeOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RechargeOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出充值订单 Excel")
    @PreAuthorize("@ss.hasPermission('reservation:recharge-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRechargeOrderExcel(@Valid RechargeOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RechargeOrderDO> list = rechargeOrderService.getRechargeOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "充值订单.xls", "数据", RechargeOrderRespVO.class,
                        BeanUtils.toBean(list, RechargeOrderRespVO.class));
    }

}